import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from '@/components/Layout/index.vue'
const routes = [
    // {
    //     path: '/',
    //     name: 'login',
    //     meta: {title: "登录页", requireAuth: true},
    //     component: () => import('@/views/login.vue')
    // },
    {
      path: '/login',
      name: 'login',
      meta: {title: "登录页", requireAuth: true},
      component: () => import('@/views/login.vue')
  },
    {
      path: '/product',
      name: 'product',
      meta: {title: "商品", requireAuth: true},
      component:Layout,
      children: [
        {
          path: '/product-add',
          name: '添加商品',
          meta: {title: "添加商品", requireAuth: true},
          component: () => import('@/views/detail/product.vue')
        },
        { 
          path: '/product-list',
          name: '商品列表',
          meta: {title: "商品列表", requireAuth: true},
          component: () => import('@/views/detail/list.vue')
        }
      ]
    },
    {
      path: '/main',
      name: 'main',
      meta: {title: "main", requireAuth: true},
      component:Layout,
      redirect: '/home',
      children: [
          { 
            path: '/home',
            name: '欢迎',
            meta: {title: "欢迎", requireAuth: true},
            component: () => import('@/views/home.vue')
          },
      ]
    },
    {
      path: '/user',
      name: '用户管理',
      meta: {title: "user", requireAuth: true},
      component:Layout,
      children: [
          { 
            path: '/user-userInfo',
            name: '用户管理',
            meta: {title: "用户管理", requireAuth: true},
            component: () => import('@/views/user/userManagement.vue')
          },
          {
            path: '/user-auth',
            name: '权限管理',
            meta: {title: "权限管理", requireAuth: true},
            component: () => import('@/views/user/auth.vue')
          }
      ]
    },
    {
      path:'/business',
      name: '商户管理',
      meta: {title: "business", requireAuth: true},
      component:Layout,
      children: [
        { 
          path: '/business-list',
          name: '商户列表',
          meta: {title: "商户列表", requireAuth: true},
          component: () => import('@/views/business/businesslist.vue')
        },
        {
          path: '/business-analysis',
          name: '商户分析',
          meta: {title: "商户分析", requireAuth: true},
          component: () => import('@/views/business/businessAnalysis.vue')
        }
    ]
    }
   
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 600, showSpinner: false })

// 设置title
router.beforeEach((to, from, next) => {
    // 启动进度条
    NProgress.start()

    // 设置头部
    if (to.meta.title) {
        document.title = to.meta.title
    } else {
        document.title = "404"
    }
    next()
})

router.afterEach(() => {
    // 关闭进度条
    NProgress.done()
})
export default router
